package com.soboot.config;

import com.soboot.handler.BaseAccessDeniedHandler;
import com.soboot.handler.BaseAuthenticationEntryPoint;
import com.soboot.security.interceptor.ValidateTokenInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.util.Base64Utils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author:
 * @createTime: 2022年09月20日 13:20:00
 * @version: 1.0
 * @Description: 权限安全配置
 * @Copyright:
 */
@Configuration
public class SecurityConfig implements WebMvcConfigurer {

    @Bean
    @ConditionalOnMissingBean(name="authenticationEntryPoint")
    public BaseAuthenticationEntryPoint authenticationEntryPoint() {
        return new BaseAuthenticationEntryPoint();
    }

    @Bean
    @ConditionalOnMissingBean(name = "accessDeniedHandler")
    public BaseAccessDeniedHandler accessDeniedHandler() {
        return new BaseAccessDeniedHandler();
    }

    @Bean
    @ConditionalOnMissingBean(value = PasswordEncoder.class)
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public ValidateTokenInterceptor validateTokenInterceptor(){
        return new ValidateTokenInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(validateTokenInterceptor());
    }
}
